<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <title>Flirt - a Flash&#153; Runtime</title>
    <link rel="stylesheet" href="style.css" type="text/css" media="screen" />
    <meta name="description" content="Flirt is an open source, extensible Flash&#153; runtime" />
    <meta name="keywords" CONTENT="Flirt, Flash, runtime, player, open source, GPL">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  </head>

<body>

<div class="title">
Flirt
</div>

<div class="subtitle">
an open source extensible Flash&#153; runtime
</div>

<div class="hr"></div>

<div class="sidebar">

	<div class="sidebarbox">
	<a class="sidebarlink" href="index.html">About</a><br>
	<a class="sidebarlink" href="render.html">Rendering</a><br>
	<a class="sidebarlink" href="script.html">Actionscript</a><br>
	Example<br>
	<a class="sidebarlink" href="extensions.html">Extension API</a><br>
	<a class="sidebarlink" href="osx.html">OS X</a><br>
	<a class="sidebarlink" href="bugs.html">Bugs</a><br>
	<a class="sidebarlink" href="future.html">Future Directions</a><br>
	</div>

<br>

	<div class="sidebarbox">
	<a class="sidebarlink" href="http://prdownloads.sourceforge.net/flirt/flirt-20040823.tgz?download">Download source</a><br>
	</div>

<br>

	<div class="sidebarbox">
	<a class="sidebarlink" href="mailto:dave@opaque.net?Subject=Flirt">Contact</a><br>
	<br>
	<a class="sidebarlink" href="http://lists.sourceforge.net/lists/listinfo/flirt-devel">Mailing list</a><br>
	<br>
	<a class="sidebarlink" href="http://sourceforge.net/projects/flirt">SourceForge project page</a><br>
	<br>
	<A href="http://sourceforge.net">
	<IMG src="http://sourceforge.net/sflogo.php?group_id=18365&amp;type=1" width="88" height="31" border="0" alt="SourceForge Logo">
	</A>
	</div>

</div>

<div class="content">

<h3>Example</h3>

<p>Here I'll step through the <code>test.c</code> example code that's in the root of the Flirt source. It's a simple command-line app that takes an SWF file name for an argument, runs it for 100 frames, and then renders out to a PNG file. As usual, we need to include a header:</p>

<p><div class="code">#include &quot;flirt-debug.h&quot;</div></p>

<p>The <code>flirt-debug.h</code> header includes the normal <code>flirt.h</code> API along with a number of functions that expose more of the guts of the Flirt runtime. These are only available when Flirt is built with <code>DD_INCLUDE_DEBUGGER</code> defined&mdash;that's currently set (along with a bunch of other handy defines) in <code>src/dd.h</code>.</p>

<p>Oh, what's all this DD stuff about? It's a 2-D library! (Yeah, yeah, I know..) I just needed a short and simple prefix for my data types, and it was the first thing that came to mind. Maybe I'll go back and change "dd" to "fl".</p>

<p>These are the only Flirt data types we'll use in this example:</p>

<p><div class="code"><pre>
ddPlayer* player;
ddImage* image;
</pre></div></p>

<p>The <code>ddPlayer</code> object represents the entire active &quot;universe&quot; for running an SWF file. It's the player. <code>ddImage</code> is the bitmap we'll render into.</p>

<p>The <code>dd_init()</code> function sets up some stuff used for debug logging. It will go away in the near future, because we don't want any globals here.</p>

<p><div class="code"><pre>
if ( !dd_init() )
  bail("dd_init() failed!\n");
</pre></div></p>

<p>The <code>dd_newImage()</code> function creates the image buffer</p>

<p><div class="code"><pre>
image = dd_newImage(WIDTH, HEIGHT);
</pre></div></p>

while the <code>dd_newPlayer_file()</code> function creates a new <code>ddPlayer</code> object, ready to read the SWF file from the given <code>FILE*</code> and use the given image as its target bitmap:

<p><div class="code"><pre>
player = dd_newPlayer_file(f, image);
</pre></div></p>

The <code>dd_setActionTraceFunction()</code> function tells the actionscript environment what function to use for its &quot;trace&quot; opcode. (It should take another void* user data arg that gets passed back into the user trace function..) Here, we'll just dump anything to stdout:

<p><div class="code"><pre>
dd_setActionTraceFunction(player, (void*)puts);
</pre></div></p>

Now we tell the player to parse the input file:

<p><div class="code"><pre>
if ( ddPlayer_readMovie(player) != 0 )
  bail("ddPlayer_readMovie() failed");
</pre></div></p>

<p>Once that's done, we step through 100 frames of the movie. Here's how it's done when Flirt is built with <code>DD_INCLUDE_DEBUGGER</code>:</p>

<p><div class="code"><pre>
for ( i = 0; i < 100; ++i )
{
  ddPlayer_step(player);

  while ( ddPlayer_executeFrameActions(player, STEP_FRAME) )
    ;

  ddPlayer_updateDisplay(player, NULL, NULL);
}
</pre></div></p>

<p><code>ddPlayer_step()</code> updates all of the active movie clips; <code>ddPlayer_executeFrameActions()</code> runs all of the active actions, either from the timelines of active clips or in enterFrame handlers; and <code>ddPlayer_updateDisplay()</code> renders the updated shapes into the player's image buffer. The last two arguments of <code>ddPlayer_updateDisplay()</code> are used to return the list and count of <code>ddRect</code> rectangles that have been updated in the image so that you can blit those to screen. Here, though, we'll just dump the entire image out to a PNG file (<code>savePNG()</code> is in <code>test.c</code>):</p>

<p><div class="code"><pre>
savePNG("test.png", image, WIDTH, HEIGHT);
</pre></div></p>

<p>Finally, we clean up the objects we've allocated:</p>

<p><div class="code"><pre>
dd_destroyPlayer(player);
dd_destroyImage(image);
</pre></div></p>

<div class="hr"></div>

    <div class="footnote">
    <b>All content copyright (C) 2004 Dave Hayden except where noted otherwise.</b>
    <br>
    <b>Macromedia and Flash are registered trademarks of Macromedia, Inc. in the United States and/or other countries.</b>
    </div>

<br>

</div>

</body>
</html>
